import React, {Component} from 'react';
import './index.less';
import {base, high} from 'nc-lightapp-front';
import getRosterData from "./functions/getRosterData";
import Refer from '../../../refer/hiref/ReportDefineGridRef';
import getLanguage from '../../language';
import do_print from "../../../../hrpub/common/utils/print";
import exportHtmlFunc from "../../../../hrpub/common/utils/exportHtml";

const language = {};

const {NCModal, NCButton, NCTable} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class RosterModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            listRefVal: {},
            columns: [],
            data: [],
            language: {}
        };
        this.closeModal = this.closeModal.bind(this);
        this.changeListRef = this.changeListRef.bind(this);
        this.print = this.print.bind(this);
    }

    componentDidMount() {
        setTimeout(() => {
            this.props.MultiInit.getMultiLang({
                moduleId: 'p6007',
                domainName: 'hrhi',
                callback: (json, status, inlt) => {
                    this.setState({
                        language: json
                    });
                }
            });
        }, 100);
    }

    getTableHeight = () => {
        return document.body.offsetHeight - 280
    };

    closeModal() {
        this.props.onClose && this.props.onClose();
        this.setState({
            listRefVal: {},
            columns: [],
            data: []
        });
    }

    changeListRef(value) {
        this.setState({
            listRefVal: value
        }, () => {
            getRosterData.call(this, this.state.language);
        });
    }

    print() {
        const {language, listRefVal, columns, data} = this.state;
        if (!listRefVal || !listRefVal.refpk) return;
        let printData = [];
        let printTmp = {
            items: []
        };

        columns.forEach((item) => {
            if (item.itemtype === "customer") return;
            printTmp.items.push({
                visible: true,
                attrcode: item.dataIndex,
                label: item.title
            });
        });

        data.forEach((item) => {
            let v = {};

            Object.keys(item).forEach((key) => {
                v[key] = {
                    value: item[key]
                };
            });
            printData.push({
                values: v
            });
        });

        do_print(document.querySelector('.roster-table'), {
            title: language['hi6007-000011'],
            maker: language['hi6007-000037'],
            date: language['hi6007-000038']
        }, {
            data: printData,
            tableTmp: printTmp
        });
    }

    output = () => {
        const {language, listRefVal, columns, data} = this.state;
        if (!listRefVal || !listRefVal.refpk) return;
        let tableMeta = {
            "moduletype": "table",
            "pagination": false,
            "code": "rosterTable",
            "name": "员工花名册列表",
            "oid": "100DZ710000000001HMC",
            items: []
        };
        columns.forEach(item => {
            if (item.itemtype === "customer") return;
            tableMeta.items.push(
                {
                    "hyperlinkflag": false,
                    "islock": false,
                    "istotal": false,
                    "width": "120px",
                    "attrcode": item.key,
                    "color": "#6E6E77",
                    "containlower": false,
                    "datatype": "1",
                    "disabled": true,
                    "editAfterFlag": false,
                    "fieldDisplayed": "refcode",
                    "fieldValued": "refpk",
                    "isDataPowerEnable": true,
                    "isnotmeta": false,
                    "isResLabel": false,
                    "isrevise": false,
                    "isShowUnit": false,
                    "itemtype": "input",
                    "label": item.title,
                    "maxlength": "40",
                    "onlyLeafCanSelect": false,
                    "position": "1",
                    "required": false,
                    "scale": "0",
                    "visible": true
                }
            )
        });
        let tableData = data.map(item => {
            let rowData = {
                status: "0",
                isOptimized: false,
                values: {}
            };
            Object.keys(item).forEach(key => {
                rowData.values[key] = {
                    display: item[key],
                    value: item[key]
                }
            });
            return rowData;
        });
        exportHtmlFunc(document.querySelector('.roster-table'), {
            title: language['hi6007-000011'],
            fileName: language['hi6007-000011'],
            maker: language['hi6007-000037'],
            date: language['hi6007-000038']
        }, {
            meta: tableMeta,
            data: tableData,
            showIndex: true
        });
    };

    render() {
        const {orgVal, visible} = this.props;
        const {listRefVal, columns, data, language} = this.state;
        return (
            <NCModal
                show={visible}
                size={columns.length > 0 ? "xlg" : "sm"}
                onHide={this.closeModal}
                backdrop={'static'}
            >
                <NCModalHeader closeButton={true}>
                    {language['hi6007-000014']}{/* 国际化处理： 花名册报表*/}
                </NCModalHeader>
                <NCModalBody>
                    <div
                        className='roster-refer'
                        style={{
                            paddingLeft: columns.length > 0 ? "0" : "34px"
                        }}
                    >
                        <Refer queryCondition={{pk_org: orgVal.refpk}}
                               value={listRefVal}
                               onChange={this.changeListRef}
                        />
                    </div>
                    <div className='roster-table'>
                        {columns.length > 0 ? <NCTable
                            columns={columns}
                            data={data}
                            scroll={{
                                x: true,
                                y: this.getTableHeight()
                            }}
                        /> : null}
                    </div>
                </NCModalBody>
                <NCModalFooter>
                    <NCButton colors="primary" onClick={this.print}>
                        {language['hi6007-000015']}{/* 国际化处理： 打印*/}
                    </NCButton>
                    <NCButton shape="border" onClick={this.output}>
                        {language['hi6007-000042']}{/* 国际化处理： 输出*/}
                    </NCButton>
                    <NCButton shape="border" onClick={this.closeModal}>
                        {language['hi6007-000016']}{/* 国际化处理： 关闭*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default RosterModal;
